<?php
/**
 * 功能.APP版本 新增/修改
 * User: xwc
 * Date: 2024/01/15
 */

namespace App\Service\Core\Sys\Config;

use App\Model\Core\Sys\AppVersionModel;
use App\Service\Core\CoreBaseService;
use App\Traits\ModelStoreTrait;

/**
 * @method $this setAppPlatform($val) app平台
 * @method $this setTargetVersion($val) 目标版本号
 * @method $this setMinVersion($val) 目标版本号
 * @method $this setAppFile($val) APP上传路径
 * @method $this setAppIntro($val) 升级文案
 * @package App\Service\Core\Sys\Config
 */
class AppVersionStoreService extends CoreBaseService
{
    use ModelStoreTrait;

    protected array $required_param = ['app_platform', 'target_version', 'min_version', 'app_file', 'app_intro'];

    /**
     * APP版本号
     * @param int $id
     * @return array
     */
    public function store(int $id = 0): array
    {
        $app_platform = trim($this->getParam('app_platform', 'android'));
        $target_version = trim($this->getParam('target_version'));
        $min_version = trim($this->getParam('min_version'));
        $app_file = trim($this->getParam('app_file'));
        $app_intro = trim($this->getParam('app_intro'));

        if (empty($app_platform)) {
            return alert_info(1, '请填写【APP平台】');
        }
        if (empty($target_version)) {
            return alert_info(1, '请填写【目标版本号】');
        }
        $pattern = '/^\d+\.\d+\.\d+$/';
        if (!preg_match($pattern, $target_version)) {
            return alert_info(1, '格式错误【目标版本号】');
        }
        if (empty($min_version)) {
            return alert_info(1, '请填写【最低版本号】');
        }
        if (!preg_match($pattern, $min_version)) {
            return alert_info(1, '格式错误【最低版本号】');
        }
        if (version_compare($target_version, $min_version) == -1) {
            return alert_info(1, '格式错误【最低版本号】');
        }
        if (empty($app_file)) {
            return alert_info(1, '未设置【APP地址】');
        }
        if (empty($app_intro)) {
            return alert_info(1, '请填写【升级文案】');
        }
        if ($this->operate_id <= 0 || empty($this->operate_by)) {
            return alert_info(1, '未设置【操作人】');
        }
        $data = [
            'app_platform' => $app_platform,
            'target_version' => $target_version,
            'min_version' => $min_version,
            'app_file' => $app_file,
            'app_intro' => $app_intro,
        ];
        $AppVersionModel = AppVersionModel::getInstance();
        if ($id > 0) {
            $AppVersionService = AppVersionService::getInstance();
            $old_info = $AppVersionService->getOne($id);
            if (empty($old_info)) {
                return alert_info(1, '记录不存在');
            }
            $change_data = easy_check_change_data($old_info, $data);
            if (empty($change_data)) {
                return alert_info(0, '操作成功', ['id' => $id]);
            }
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $this->operate_by;
            $up_res = $AppVersionModel->updateOne($id, $data);
            if (!$up_res) {
                return alert_info(1, '更新失败');
            }
            $msg = '修改APP版本号';
        } else {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['created_by'] = $this->operate_by;
            $data['updated_at'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $this->operate_by;
            $add_res = $AppVersionModel->addOne($data);
            if (!$add_res) {
                return alert_info(1, '添加失败');
            }
            $msg = '新增APP版本号';
        }
        $this->addLog('APP版本号', $msg . ' id:' . $id, $this->operate_id, $this->operate_by);
        return alert_info(0, '操作成功');
    }
}
